package com.longrise.android.byjk.plugins.dealsituation.course.certificate;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.common.base.BaseActivity;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoConsts;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.params.VideoParams;
import com.longrise.android.byjk.widget.view.ZoomImageView;

/**
 * Created by godliness on 2017/4/27.
 * 培训证书与培训记录表
 */

public class CertActivity extends BaseActivity<CertPresenter> implements CertView, View.OnClickListener {

    private static final String TAG = "CertActivity";

    private final String SAVE_URL = "save_url";

    public interface CertType {

        int TYPE_CERT = 0;

        int TYPE_TABLE = 1;
    }

    /**
     * toolbar栏
     */
    private Toolbar mToolbar;
    private TextView mTvSave;
    private View mRightView;

    /**
     * 图片展示区
     */
    private ZoomImageView mIvCert;
    private ImageView mIvSave;
    private ProgressBar mPbLoading;
    private TextView mTvUpload;

    private View mLoadView;

    private VideoParams mParams;
    private String mUrl;

    @Override
    public int getContentViewId(@Nullable Bundle sate) {
        if (sate != null) {
            onRestoreState(sate);
        } else {
            getExtraData();
        }
        return R.layout.activity_cert;
    }

    private void getExtraData() {
        Intent extraIntent = getIntent();
        mParams = extraIntent.getParcelableExtra(VideoConsts.VIDEO_PARAMS_PARCELABLE);
    }

    private void initTitle() {
        mToolbar = (Toolbar) findViewById(R.id.byjk_toolbar);
        mToolbar.setTitle(mParams.mType == CertType.TYPE_CERT ? getString(R.string.curriulunm_zhengshu) : getString(R.string.currilunm_jilu));
        mTvSave = (TextView) findViewById(R.id.iv_toolbar_right_tv);
        mTvSave.setText("保存");
        mRightView = findViewById(R.id.bb_toolbar_right_rl_tv);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void initView() {
        initTitle();
        mIvCert = (ZoomImageView) findViewById(R.id.iv_cert);
        mIvSave = (ImageView) findViewById(R.id.iv_save);
        mPbLoading = (ProgressBar) findViewById(R.id.pb_cert);
        mTvUpload = (TextView) findViewById(R.id.tv_uploadrecord);
        mLoadView = findViewById(R.id.rela_load_xqpc);
        mLoadView.setVisibility(View.GONE);
        regEvent(true);

        toRequest();
    }

    /**
     * 如果课程考核通过，则不显示
     */
    private void setUploadBtnVisiableState() {
        if (mParams != null) {
            mTvUpload.setVisibility(mParams.mFinish ? View.GONE : View.VISIBLE);
        }
    }

    private void regEvent(boolean isClick) {
        if (mTvSave != null) {
            mTvSave.setOnClickListener(isClick ? this : null);
        }
        if (mTvUpload != null) {
            mTvUpload.setOnClickListener(isClick ? this : null);
        }
    }

    private void toRequest() {
        if (mParams == null) {
            return;
        }
        //bbt_course_sTrainCertImg培训证书   bbt_course_sTrainRecordImg培训记录表
        if (TextUtils.isEmpty(mUrl)) {
            mPresenter.loadImage();
            return;
        }
        mPresenter.toLoadImage(mUrl);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_toolbar_right_tv:
                mPresenter.saveToFile(mIvSave);
                break;

            case R.id.tv_uploadrecord:
                mPresenter.toUploadRecord();
                break;

            default:
                break;
        }
    }

    @Override
    public void setImageDrawable(Drawable drawable) {
        if (mIvCert != null) {
            mIvCert.setImageDrawable(drawable);
            mIvCert.setVisibility(View.VISIBLE);
            AlphaAnimation alphaToVisiable = alphaToVisiable();
            mIvCert.setAnimation(alphaToVisiable);
            alphaToVisiable.start();

        }
        if (mIvSave != null) {
            mIvSave.setImageDrawable(drawable);
        }
        if (mRightView != null) {
            mRightView.setVisibility(View.VISIBLE);
        }
        setUploadBtnVisiableState();
    }

    private AlphaAnimation alphaToVisiable() {
        AlphaAnimation alphaToVisiable = new AlphaAnimation(0, 1);
        alphaToVisiable.setDuration(300);
        return alphaToVisiable;
    }

    @Override
    public VideoParams getParams() {
        return mParams;
    }

    @Override
    public void onError() {
        dismissLoadingDialog();
    }

    @Override
    public int getCertType() {
        return mParams.mType;
    }

    @Override
    public void parse(String url) {
        mUrl = url;
    }

    @Override
    public void updateUploadBtnState(boolean isClick) {
        if (mTvUpload != null) {
            mTvUpload.setClickable(isClick);
            mTvUpload.setTextColor((isClick ? Color.parseColor("#FFFFFF") : Color.parseColor("#999999")));
        }
    }

    @Override
    public void showLoadingDialog() {
        if (mLoadView != null) {
            mLoadView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void dismissLoadingDialog() {
        if (mPbLoading != null) {
            mPbLoading.setVisibility(View.GONE);
        }
        if (mLoadView != null) {
            mLoadView.setVisibility(View.GONE);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable(VideoConsts.VIDEO_PARAMS_PARCELABLE, mParams);
        outState.putString(SAVE_URL, mUrl);
    }

    private void onRestoreState(Bundle state) {
        if (state != null) {
            mParams = state.getParcelable(VideoConsts.VIDEO_PARAMS_PARCELABLE);
            mUrl = state.getString(SAVE_URL);
        }
    }

    @Override
    protected void onDestroy() {
        mPresenter.setFinished();
        super.onDestroy();
    }
}
